//
// ********************************************************************
// * License and Disclaimer                                           *
// *                                                                  *
// * The  Geant4 software  is  copyright of the Copyright Holders  of *
// * the Geant4 Collaboration.  It is provided  under  the terms  and *
// * conditions of the Geant4 Software License,  included in the file *
// * LICENSE and available at  http://cern.ch/geant4/license .  These *
// * include a list of copyright holders.                             *
// *                                                                  *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work  make  any representation or  warranty, express or implied, *
// * regarding  this  software system or assume any liability for its *
// * use.  Please see the license in the file  LICENSE  and URL above *
// * for the full disclaimer and the limitation of liability.         *
// *                                                                  *
// * This  code  implementation is the result of  the  scientific and *
// * technical work of the GEANT4 collaboration.                      *
// * By using,  copying,  modifying or  distributing the software (or *
// * any work based  on the software)  you  agree  to acknowledge its *
// * use  in  resulting  scientific  publications,  and indicate your *
// * acceptance of all terms of the Geant4 Software license.          *
// ********************************************************************
//
//
// $Id$
// 
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

// Make this appear first!

#include "G4Timer.hh"
#include "MRunAction.hh"
#include "MRunMessenger.hh"
#include "Log.hh"
#include "G4Run.hh"
#include "MDetectorConstruction.hh"

#include "TFile.h"
#include "TTree.h"
#include "TBranch.h"

#include <fstream>

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

  extern G4int radiator;
  extern G4int window_Material;
  extern G4double window_Thickness;
  extern G4double H2O_ppm;
  extern G4double Oxygen_ppm;
  extern G4int MRT_open;

  int MRunAction::Event;
  double MRunAction::Omit_x;
  double MRunAction::Omit_y;
  double MRunAction::Omit_z;
  double MRunAction::dirX;
  double MRunAction::dirY;
  double MRunAction::dirZ;
  double MRunAction::KEnergy;
  double MRunAction::Final_x;
  double MRunAction::Final_y;
  double MRunAction::Final_z;
  double MRunAction::ParentID;
  double MRunAction::globalTime;
  double MRunAction::localTime;
  TTree * MRunAction::OpticalPhoton;

  int MRunAction::Parent_Event;
  double MRunAction::Parent_Omit_KEnergy;
  double MRunAction::Parent_Omit_x;
  double MRunAction::Parent_Omit_y;
  double MRunAction::Parent_Omit_z;
  double MRunAction::Parent_dirX;
  double MRunAction::Parent_dirY;
  double MRunAction::Parent_dirZ;
  double MRunAction::Parent_KEnergy;
  double MRunAction::Parent_Final_x;
  double MRunAction::Parent_Final_y;
  double MRunAction::Parent_Final_z;
  double MRunAction::Parent_KEnergy2;
  double MRunAction::Parent_Final_x2;
  double MRunAction::Parent_Final_y2;
  double MRunAction::Parent_Final_z2;
  double MRunAction::Parent_globalTime;
  double MRunAction::Parent_localTime;
  TTree * MRunAction::Parent;


MRunAction::MRunAction()
{
  timer = new G4Timer;
  runMessenger = new MRunMessenger(this);

  fileName="output.root";
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

MRunAction::~MRunAction()
{
  delete timer;
}

void MRunAction::SetFileName(G4String _fileName)
{
   fileName = _fileName;
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void MRunAction::BeginOfRunAction(const G4Run* aRun)
{
//  logfile.open("../Log/Log.dat", ios_base::in | ios_base::out);
//  if (logfile.is_open())
//  {
// 	//cout << "Log file opened!" << endl;
//	logfile >> logNum;
//	cout << "Log file created: Log.txt" << endl;
//	logfile.seekg(0,logfile.beg);
//	if (logNum >= 9999){ logfile << setw(4) << setfill('0') << 0 << "\n"; }
//	else {logfile << setw(4) << setfill('0') <<(logNum + 1) << "\n";}
//	logNum = logNum + 1;
//	cout << "Log Number: " << logNum << endl;
//  }
//  else {cout << "Log file does not exist!" << endl; exit(0);}
//  logfile.close();
//
//  G4int number = aRun -> GetNumberOfEvent();
//
//  char buffer[50];
//  sprintf(buffer, "../Log/Log_%d.out", logNum);
//  cout << buffer << endl;
//  loginfo.open(buffer, ios_base::out);
//  if (loginfo.is_open())
//  {
//	//loginfo << "Number of Event of this Run : " << number << G4endl;
//	//loginfo << "\n";
//	loginfo << "radiator: " << MDetectorConstruction::radiator << G4endl;
//	loginfo << "window_Material: " << MDetectorConstruction::window_Material << G4endl;
//	loginfo << "window_Thickness: " << MDetectorConstruction::window_Thickness << G4endl;
//	loginfo << "H2O_ppm: " << MDetectorConstruction::H2O_ppm << G4endl;
//	loginfo << "Oxygen_ppm: " << MDetectorConstruction::Oxygen_ppm << G4endl;
//	loginfo << "MRT_open:" << MDetectorConstruction::MRT_open << G4endl;
//	loginfo << "\n";
//  }
//
////  fstream macfile;
////  string str_mac;
////  macfile.open("../optPhoton.mac", ios_base::in);
////  loginfo << "mac file:" << G4endl;
////  while (!macfile.eof())
////  {
////  	getline(macfile, str_mac);
////  	loginfo << str_mac << endl;
////  }
////
////  loginfo.close();  
////  macfile.close();
//
//  char ff[50];
//  sprintf(ff, "./Photon_%d.root", logNum);
//  cout << ff << endl;
  f = new TFile(fileName, "RECREATE");

  MRunAction::OpticalPhoton = new TTree("CerenkovPosition", "OpticalPhoton");
  MRunAction::OpticalPhoton->Branch("Event",	&Event);
  MRunAction::OpticalPhoton->Branch("Omit_x",   &Omit_x);
  MRunAction::OpticalPhoton->Branch("Omit_y",   &Omit_y);
  MRunAction::OpticalPhoton->Branch("Omit_z",   &Omit_z);
  MRunAction::OpticalPhoton->Branch("Dir_X",   &dirX);
  MRunAction::OpticalPhoton->Branch("Dir_Y",   &dirY);
  MRunAction::OpticalPhoton->Branch("Dir_Z",   &dirZ);
  MRunAction::OpticalPhoton->Branch("X",   &Final_x);
  MRunAction::OpticalPhoton->Branch("Y",   &Final_y);
  MRunAction::OpticalPhoton->Branch("Z",   &Final_z);
  MRunAction::OpticalPhoton->Branch("KEnergy",   &KEnergy);
  MRunAction::OpticalPhoton->Branch("ParentID",  &ParentID);
  MRunAction::OpticalPhoton->Branch("globalTime",  &globalTime);
  MRunAction::OpticalPhoton->Branch("localTime",  &localTime);

  MRunAction::Parent = new TTree("Parent", "Parent");
  MRunAction::Parent->Branch("Parent_Event",	&Event);
  MRunAction::Parent->Branch("Parent_Omit_KEnergy",   &Parent_Omit_KEnergy);
  MRunAction::Parent->Branch("Parent_Omit_x",   &Parent_Omit_x);
  MRunAction::Parent->Branch("Parent_Omit_y",   &Parent_Omit_y);
  MRunAction::Parent->Branch("Parent_Omit_z",   &Parent_Omit_z);
  MRunAction::Parent->Branch("Parent_dirX",   &Parent_dirX);
  MRunAction::Parent->Branch("Parent_dirY",   &Parent_dirY);
  MRunAction::Parent->Branch("Parent_dirZ",   &Parent_dirZ);
  MRunAction::Parent->Branch("Parent_KEnergy",   &Parent_KEnergy);
  MRunAction::Parent->Branch("Parent_Final_x",   &Parent_Final_x);
  MRunAction::Parent->Branch("Parent_Final_y",   &Parent_Final_y);
  MRunAction::Parent->Branch("Parent_Final_z",   &Parent_Final_z);
  MRunAction::Parent->Branch("Parent_KEnergy2",   &Parent_KEnergy2);
  MRunAction::Parent->Branch("Parent_Final_x2",   &Parent_Final_x2);
  MRunAction::Parent->Branch("Parent_Final_y2",   &Parent_Final_y2);
  MRunAction::Parent->Branch("Parent_Final_z2",   &Parent_Final_z2);
  MRunAction::Parent->Branch("Parent_globalTime",  &Parent_globalTime);
  MRunAction::Parent->Branch("Parent_localTime",  &Parent_localTime);

  //G4cout << "### Run " << aRun->GetRunID() << " start." << G4endl; 
  timer->Start();
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void MRunAction::EndOfRunAction(const G4Run* aRun)
{   
  timer->Stop();
  G4int number = aRun -> GetNumberOfEvent();
  G4cout << "number of event = " << number 
         << " " << *timer << G4endl;

 
  MRunAction::OpticalPhoton -> Write();
  MRunAction::Parent -> Write();

  f -> Close();
}
//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
